package gui.component;

import database.entity.Mail;
import database.entity.User;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
/**
 *
 * @author Tom
 */
public class MailRenderer implements ListCellRenderer<Mail>
{
    User user;
    public MailRenderer(User user)
    {
        this.user=user;
    }

    @Override
    public Component getListCellRendererComponent(JList<? extends Mail> jlist, Mail e, int i, boolean selected, boolean bln1) 
    {        
        JPanel pnMail = new JPanel(new BorderLayout());
        JPanel pnLeft = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel pnRight = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        pnMail.add(pnLeft,BorderLayout.WEST);
        pnMail.add(pnRight,BorderLayout.EAST);
        
        //get check
        JCheckBox chbSelected = new JCheckBox();        
        
        JLabel lbSenderOrReceiver = new JLabel();      
        lbSenderOrReceiver.setOpaque(true);
        lbSenderOrReceiver.setBackground(Color.WHITE);
        lbSenderOrReceiver.setPreferredSize(new Dimension(250, 25));
        //check sender/receiver
        if (e.To.contains(user.Email))
        {
            //this mail is received, show sender
            lbSenderOrReceiver.setText(user.databaseWorker.getContactNameFromEmail(e.From,false));
        }
        else
        {
            lbSenderOrReceiver.setText(user.databaseWorker.getContactNameFromEmail(e.To,false));
        }
        
        //get subject
        JLabel lbSubject = new JLabel(e.Subject);     
        
        //set font for subject
        if (e.Unread)
            lbSubject.setFont(PreloadedFont.TIMES_NEWS_ROMAN_BOLD_16);
        else
            lbSubject.setFont(PreloadedFont.TIMES_NEWS_ROMAN_PLAIN_14);
        
        //set font for address
        lbSenderOrReceiver.setFont(PreloadedFont.TIMES_NEWS_ROMAN_PLAIN_14);
        
        pnLeft.add(chbSelected);
        pnLeft.add(lbSenderOrReceiver);
        pnLeft.add(lbSubject);
        
        if (selected)
        {
            chbSelected.setSelected(true);
            e.selected=true;
            pnLeft.setOpaque(true);
            pnRight.setOpaque(true);
            pnLeft.setBackground(Color.LIGHT_GRAY);
            pnRight.setBackground(Color.LIGHT_GRAY);
        }        
        return pnMail;                
    }   
}
